//
//  HMHomeModel.m
//  G05爱鲜蜂
//
//  Created by 源 何 on 16/10/16.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMHomeModel.h"

@interface HMHomeModel ()

@end

@implementation HMHomeModel

- (instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)modelWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

- (void)setValue:(id)value forKey:(NSString *)key{
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"id"]) {
        _ids = value;
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}

+ (void)downloadWithKey:(NSString *)key andSuccessBlock:(void (^)(NSArray *))successBlock andFailureBlock:(void (^)())failureBlock{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"1" forKey:@"call"];
    
    [DSHTTPClient postUrlString:@"focus.json.php" withParam:param withSuccessBlock:^(id data) {
//        NSLog(@"%@",data);
        
        NSMutableArray *modelArray = [NSMutableArray array];
        
        NSDictionary *dataDict = data[@"data"];
        
        NSArray *dataArray = dataDict[key];
        
        for (NSDictionary *dict in dataArray) {
            
            HMHomeModel *model = [HMHomeModel modelWithDict:dict];
            
            if ([key isEqualToString:@"focus"]) {
                
                [modelArray addObject:model];
            } else if ([key isEqualToString:@"icons"]) {
                [modelArray addObject:model];
            } else {
                [modelArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            successBlock(modelArray.copy);
        });
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
        failureBlock();
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
        failureBlock();
    }];
}



@end
